import 'dart:io';

import 'package:flutter/material.dart';
import 'package:get/get_state_manager/src/rx_flutter/rx_obx_widget.dart';
import 'package:get/get_state_manager/src/simple/get_view.dart';
import 'package:photo_manager/photo_manager.dart';
import 'package:photo_view/photo_view.dart';
import 'package:video_player/video_player.dart';
import 'package:vischat/core/annotations/getx_route_enhanced.dart';
import 'package:vischat/modules/media/media_preview_binding.dart';
import 'package:vischat/modules/media/media_preview_controller.dart';

@GetXRoute('/media_preview', binding: MediaPreviewBinding)
class MediaPreviewPage extends GetView<MediaPreviewController> {
  const MediaPreviewPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      body: SafeArea(
        child: Obx(
          () => PageView.builder(
            controller: controller.pageController,
            itemCount: controller.mediaList.length,
            onPageChanged: controller.onPageChanged,
            itemBuilder: (context, index) {
              final asset = controller.mediaList[index];

              if (asset.type == AssetType.image) {
                return FutureBuilder<File?>(
                  future: asset.file,
                  builder: (context, snapshot) {
                    if (snapshot.hasData) {
                      return PhotoView(
                        imageProvider: FileImage(snapshot.data!),
                        minScale: PhotoViewComputedScale.contained,
                        maxScale: PhotoViewComputedScale.covered * 2,
                      );
                    } else {
                      return Center(child: CircularProgressIndicator());
                    }
                  },
                );
              } else if (asset.type == AssetType.video) {
                return _buildVideoPlayer();
              } else {
                return Center(child: Text('不支持的媒体类型'));
              }
            },
          ),
        ),
      ),
    );
  }

  Widget _buildVideoPlayer() {
    return Obx(() {
      if (controller.videoController == null ||
          !controller.videoController!.value.isInitialized) {
        return Center(child: CircularProgressIndicator());
      }

      return Center(
        child: AspectRatio(
          aspectRatio: controller.videoController!.value.aspectRatio,
          child: Stack(
            alignment: Alignment.bottomCenter,
            children: [
              VideoPlayer(controller.videoController!),
              _buildVideoControls(),
            ],
          ),
        ),
      );
    });
  }

  Widget _buildVideoControls() {
    return Obx(
      () => Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          VideoProgressIndicator(
            controller.videoController!,
            allowScrubbing: true,
            colors: VideoProgressColors(
              playedColor: Colors.red,
              bufferedColor: Colors.grey,
              backgroundColor: Colors.white24,
            ),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              IconButton(
                icon: Icon(
                  controller.isVideoPlaying ? Icons.pause : Icons.play_arrow,
                  color: Colors.white,
                  size: 36,
                ),
                onPressed: controller.playVideo,
              ),
            ],
          ),
        ],
      ),
    );
  }
}
